package com.imc.platform.base.user.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.imc.api.order.pojo.UserOrgAccount;
import com.imc.core.utils.MysqlUtil;
import com.imc.platform.base.org.service.IOrgService;
import com.imc.platform.base.user.dao.mapper.IUserMapper;
import com.imc.platform.base.user.pojo.*;
import com.imc.platform.base.user.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@Service
@Transactional
public class UserServiceImpl implements IUserService {

    @Autowired
    PasswordEncoder passwordEncoder;

    @Autowired
    IUserMapper userMapper;
    @Autowired
    IOrgService orgService;

    @Autowired
    MysqlUtil mysqlUtil;
    /**
    *QY 2018/9/3 10:15
    *方法描述：查询所有用户
    **/
    @Override
    public PageInfo<User> queryAll(int pageNumber, int pageSize) {
        PageHelper.startPage(pageNumber,pageSize,true);
        List<Map> mapList = userMapper.queryAll();
        for(Map map : mapList){
            map.put("pageNumber",pageNumber);
            map.put("pageSize",pageSize);
        }
        PageInfo info= new PageInfo<>(mapList);
        return info;
    }

    @Override
    public User queryUserById(String userId) {
        User user = userMapper.queryUserById(userId);
        user.setOrgId(orgService.queryOrgIdByUserId(userId));
        user.setOrgName(orgService.queryOrgNameByOrgId(user.getOrgId()));
        Map<String,Object>parms=new HashMap<>();
        parms.put("userId",userId);
        parms.put("orgId",user.getOrgId());
        user.setUserOrgAccount(userMapper.queryOrderByUserId(parms));
        return user;
    }

    @Override
    public UserSub queryUserOrgAccountByOrgIdAndParentId(String orgId, String parentId) {
        return userMapper.queryUserOrgAccountByOrgIdAndParentId(orgId,parentId);
    }

    @Override
    public int updateUserOrgAccountLessonAccount(String orgId, String parentId, String lessonAccount) {
        int count = userMapper.updateUserOrgAccountLessonAccount(orgId,parentId,lessonAccount,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        if(count == 0){
            UserOrgAccount userOrgAccount = new UserOrgAccount();
            userOrgAccount.setOrgId(orgId);
            userOrgAccount.setLessonAccount(Integer.parseInt(lessonAccount));
            userOrgAccount.setUserId(parentId);
            count = userMapper.insertUserOrgAccountLessonAccount(userOrgAccount);
        }
        return count;
    }

    @Override
    public String addUser(Map<String,Object> parms) {
        List<String>parmList=Arrays.asList(parms.get("orgId").toString().split(","));
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        parms.put("createTime",df.format(new Date()));
        parms.put("id",mysqlUtil.getMaxId("t_user"));
        parms.put("loginpwd",passwordEncoder.encode(parms.get("loginpwd").toString()));
        if(userMapper.addUser(parms)>0){
            Map<String,Object> resMap=new HashMap<String,Object>();
            resMap.put("userId",parms.get("id").toString());
            resMap.put("parmList",parmList);
            //根据用户ID查询
            if(userMapper.queryUserOrgByUserId(parms.get("id").toString()).size()>0){
                if(userMapper.delUserOrgByUserId(parms.get("id").toString())>0){
                    userMapper.addUserOrgList(resMap);
                }
            }else{
                userMapper.addUserOrgList(resMap);
            }
        }
        return parms.get("id").toString();
    }

    @Override
    public int delUserById(String userId) {
        userMapper.delUserRoleByUserId(userId);
        userMapper.delUserOrgByUserId(userId);
        return userMapper.delUserById(userId);
    }

    @Override
    public int delUserSubById(String id) {
        return userMapper.delUserSubById(id);
    }

    /**
    *QY 2018/9/3 10:16
    *方法描述：更改状态
    **/
    @Override
    public int updateState(String state, String userId) {
        if("false".equals(state)){
            state="2";
        }else if("true".equals(state)){
            state="1";
        }
        return userMapper.updateState(state,userId);
    }

    @Override
    public int updateUserByUserId(Map<String, Object> parms) {
        List<String>parmList=Arrays.asList(parms.get("orgId").toString().split(","));
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
//        parms.put("createTime",df.format(new Date()));
//        parms.put("loginpwd",parms.get("loginpwd").toString());
        Map<String, Object> resMap = new HashMap<String, Object>();
        resMap.put("userId", parms.get("id").toString());
        resMap.put("parmList", parmList);
        if(userMapper.updateUserByUserId(parms)>0){
            if(userMapper.queryUserOrgByUserId(parms.get("id").toString()).size()>0) {
                if (userMapper.delUserOrgByUserId(parms.get("id").toString()) > 0) {
                    return userMapper.addUserOrgList(resMap);
                }
            }else {
                return userMapper.addUserOrgList(resMap);
            }
        }
        return 0;
    }

    @Override
    public int queryUserByLoginName(String loginName) {
        if(userMapper.queryUserByLoginName(loginName).size()>0){
            return 1;
        }
        return 0;
    }

    @Override
    public PageInfo<User> queryUserByUserName(int pageNumber, int pageSize, String select,String duty,String orgId,String type) {
        PageHelper.startPage(pageNumber,pageSize,true);
        PageInfo info= new PageInfo<>(userMapper.queryUserByUserName2(select,null,duty,orgId,type));
        return info;
    }

    @Override
    public List<String> queryUserOrgByUserId(String UserId) {
        return userMapper.queryUserOrgByUserId(UserId);
    }

    @Override
    public void addUserAttach(UserAttach userAttach) {
        userMapper.addUserAttach(userAttach);
    }

    @Override
    public UserAttach queryUserDescAllByUserId(String userId) {
        List<UserAttach> userAttachList= userMapper.queryUserDescAllByUserId(userId);
        UserAttach userAttach=new UserAttach();
        List<String> userProUrlList = new ArrayList<>();
        for(UserAttach attach : userAttachList) {
            userProUrlList.add(attach.getUserProUrl());
            userAttach.setUserId(attach.getUserId());
            userAttach.setUserProfile(attach.getContent());
        }
        userAttach.setUserProUrlList(userProUrlList);
        return userAttach;
    }

    @Override
    public void updateUserDesc(User user) {
        userMapper.updateUserDesc(user);
    }

    @Override
    public void delUserAttach(String id, String imgUrl) {
        userMapper.delUserAttach(id,imgUrl);
    }

    @Override
    public int insertUserCountLog(UserCountLog userCountLog) {
        return userMapper.insertUserCountLog(userCountLog);
    }

    @Override
    public UserOrgAccount queryOrderById(String id) {
        return userMapper.queryOrderById(id);
    }

    @Override
    public PageInfo<Map<String,Object>> queryUserSubById(String id, Pageable pageable,String orgId,String currentUserId,String dutyFlag) {
        PageHelper.startPage(pageable.getPageNumber(),pageable.getPageSize());
        val data = userMapper.queryUserSubById(id,orgId,currentUserId,dutyFlag);
        PageInfo info= new PageInfo<>(data);
        return info;
    }

    @Override
    public User queryUserByLoginName1(String loginName) {
        return userMapper.queryUserByLoginName1(loginName);
    }

    @Override
    public UserOrgAccount queryUserOrgAccount(String userId) {
        return userMapper.queryUserOrgAccount(userId);
    }

    @Override
    public User queryRoleUserByLoginName(String loginName) {
        return userMapper.queryRoleUserByLoginName(loginName);
    }

    @Override
    public PageInfo<Map<String,Object>> getUserACountById(Pageable pageable, String userCountId) {
        PageHelper.startPage(pageable.getPageNumber(),pageable.getPageSize());
        val data = userMapper.getUserACountById(userCountId);
        PageInfo info=new PageInfo(data);
        return info;
    }

    @Override
    public int updateLessonAccountById(UserOrgAccount userOrgAccount) {
          return userMapper.updateLessonAccountById(userOrgAccount);
    }

    @Override
    public int insertLessonAccount(UserOrgAccount userOrgAccount) {
          return userMapper.insertUserOrgAccountLessonAccount(userOrgAccount);
    }
}
